package com.bootdo.collection.redis;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.collection.domain.PictureModelDO;
import com.bootdo.collection.service.impl.PictureModelService;
import com.bootdo.collection.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从队列中取出爬虫爬取的数据
 */
@Component
public class Comsumer implements CommandLineRunner {
    @Autowired
    private PictureModelService pictureModelService;
    @Override
    public void run(String... args) throws Exception {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                JedisUtil.init();
                while (true){
                    //即使抛异常也不会跳出循环
                    try{
                        PictureModelDO pictureModelDO = JSONObject.parseObject(JedisUtil.brpop("PICJSON"),PictureModelDO.class);
                        pictureModelService.add(pictureModelDO,pictureModelDO.getStationId());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        t1.start();
    }
}
